﻿//绑定回车事件
function checkNumber() {
    return (event.keyCode >= 48 && event.keyCode <= 57) || event.keyCode == 46 || event.keyCode == 45;
}
function formatNumber(number, decimalPlaces) {
    number = number.toString().replace(/\,/g, "");
    if (isNaN(number) || number == "") return "";
    if (decimalPlaces == 0) {
        return parseInt(number);
    }
    var _decimalPlaces = 2;
    if (typeof (decimalPlaces) == 'number') {
        _decimalPlaces = decimalPlaces > 12 ? 12 : decimalPlaces; //最大12位
    }
    number = Math.round(number * Math.pow(10, _decimalPlaces)) / Math.pow(10, _decimalPlaces);
    if (number < 0)
        return '-' + formatDollars(Math.floor(Math.abs(number) - 0) + '') + formatCents(Math.abs(number) - 0, _decimalPlaces);
    else
        return formatDollars(Math.floor(number - 0) + '') + formatCents(number - 0, _decimalPlaces);
}
function formatDollars(number) {
    if (number.length <= 3)
        return (number == '' ? '0' : number);
    else {
        var mod = number.length % 3;
        var output = (mod == 0 ? '' : (number.substring(0, mod)));
        for (i = 0; i < Math.floor(number.length / 3); i++) {
            if ((mod == 0) && (i == 0))
                output += number.substring(mod + 3 * i, mod + 3 * i + 3);
            else
                output += number.substring(mod + 3 * i, mod + 3 * i + 3);
        }
        return (output);
    }
}
function formatCents(amount, decimalPlaces) {
    var _decimalPlaces = 0;
    if (typeof (decimalPlaces) == 'number') {
        _decimalPlaces = decimalPlaces;
    }
    if (decimalPlaces == 0) return "";
    amount = Math.round((amount - Math.floor(amount)) * Math.pow(10, _decimalPlaces)) / Math.pow(10, _decimalPlaces);
    if (amount == 0) {
        return ".".PadRight(_decimalPlaces + 1, '0');
    }
    if (amount.toString().substring(2).length < _decimalPlaces) {
        if (amount.toString().substring(2).length < 2)
            return amount.toString().substring(1).PadRight(3, '0');
        return amount.toString().substring(1);
    }
    return amount.toString().substring(1).PadRight(_decimalPlaces + 1, '0');
}
String.prototype.PadLeft = function (totalWidth, paddingChar) {
    if (paddingChar != null) {
        return this.PadHelper(totalWidth, paddingChar, false);
    } else {
        return this.PadHelper(totalWidth, ' ', false);
    }
}
String.prototype.PadRight = function (totalWidth, paddingChar) {
    if (paddingChar != null) {
        return this.PadHelper(totalWidth, paddingChar, true);
    } else {
        return this.PadHelper(totalWidth, ' ', true);
    }
}
String.prototype.PadHelper = function (totalWidth, paddingChar, isRightPadded) {
    if (this.length < totalWidth) {
        var paddingString = new String();
        for (i = 1; i <= (totalWidth - this.length); i++) {
            paddingString += paddingChar;
        }

        if (isRightPadded) {
            return (this + paddingString);
        } else {
            return (paddingString + this);
        }
    } else {
        return this;
    }
}
